#!/bin/bash

echo "[INSTALL] Installing HTTP Proxy locally..."
echo

# Get GOPATH
GOPATH=$(go env GOPATH)

if [ -z "$GOPATH" ]; then
    echo "[ERROR] GOPATH not found. Please ensure Go is properly installed."
    exit 1
fi

echo "[INFO] GOPATH: $GOPATH"

# Create bin directory if it doesn't exist
if [ ! -d "$GOPATH/bin" ]; then
    echo "[ACTION] Creating $GOPATH/bin directory..."
    mkdir -p "$GOPATH/bin"
fi

# Build the executable
echo "[ACTION] Building HTTP Proxy..."
go build -o http-proxy ./cmd/http-proxy

if [ $? -ne 0 ]; then
    echo "[ERROR] Failed to build HTTP Proxy"
    exit 1
fi

# Copy to GOPATH/bin
echo "[ACTION] Installing to $GOPATH/bin..."
cp http-proxy "$GOPATH/bin/http-proxy"

if [ $? -eq 0 ]; then
    echo "[SUCCESS] HTTP Proxy installed successfully!"
    echo
    echo "You can now use 'http-proxy' command globally."
    echo "Example: http-proxy"
    echo
    echo "Note: Make sure your GOPATH/bin is in your system PATH."
    echo "Current GOPATH/bin: $GOPATH/bin"
else
    echo "[ERROR] Failed to install HTTP Proxy"
    exit 1
fi

echo
echo "[INFO] Installation completed!"